<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitLabelCtl;
use Srv\Apps\Proto\DTO\Skit\SkitLabelAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitLabelEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitLabelListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitLabelListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitLabelAct extends ActionAbstract
{
    /**
     * @url /skit/label/list
     * @auth skitLabelList
     * @req Srv.Apps.Proto.DTO.Skit.SkitLabelListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitLabelListVO
     * 短剧标签列表
     */
    public function getSkitLabelList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitLabelListDTO) || !($VOClass instanceof SkitLabelListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitLabelCtl::tag()->getSkitLabelList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/label/add
     * @auth skitLabelAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitLabelAddDTO
     * 短剧标签新增
     */
    public function skitLabelAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitLabelAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $sklbId     = SkitLabelCtl::tag()->skitLabelAdd($setData);
        if($sklbId < 1){
            $errMsg = '短剧标签新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_LABEL_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '短剧标签新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_LABEL_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/label/edit
     * @auth skitLabelEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitLabelEditDTO
     * 短剧标签编辑
     */
    public function skitLabelEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitLabelEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $sklbId         = intval($DTOClass->getSklbId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitLabelCtl   = SkitLabelCtl::tag();
        $labelSrc       = $SkitLabelCtl->getSkitLabelSrc($sklbId);
        if(!isset($labelSrc['sklb_id'])){
            $logData    = $setData;
            $errMsg     = '短剧标签不存在';
            $LogCtl->logAdd(LogCtl::SKIT_LABEL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
        }
        $result         = $SkitLabelCtl->skitLabelEdit($sklbId, $setData);
        $logData        = ['new' => $setData, 'old' => $labelSrc];
        if(!$result) {
            $errMsg     = '短剧标签编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_LABEL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '短剧标签编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_LABEL_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}